﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Admin.Module.BaseModule
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "PageView", useExceptionHandler: true);
            PermissionBLL.CheckRoleThrowException(role: "PageEdit", useExceptionHandler: true);
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BindData();
            }
        }
        protected void BindData()
        {
            this.Store1.DataSource = BitPortal.Business.ModuleBLL.GetAllBaseModule();
            this.Store1.DataBind();
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
        }
        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindData();
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            wAddEdit.AutoLoad.Url = "BaseModuleAddEdit.aspx?ReloadID=" + GridPanel1.ClientID;
            wAddEdit.LoadContent();
            wAddEdit.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                wAddEdit.AutoLoad.Url = "BaseModuleAddEdit.aspx?BaseModuleID=" + sm.SelectedRows[0].RecordID + "&ReloadID=" + GridPanel1.ClientID;
                wAddEdit.LoadContent();
                wAddEdit.Show();
            }
        }
        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.ModuleBLL.DeleteBaseModuleByID(row.RecordID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " Module thành công!");
                    BindData(); 
                    sm.SelectedRows.Clear();
                }
                
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
    }
}